import apiClient from '@/api/axios.js';

/**
 * 评论API类
 * 
 * 评论数据结构:
 * {
 *   commentID: Number,     // 评论ID
 *   articleID: Number,     // 文章ID
 *   userID: Number,        // 用户ID
 *   parentCommentID: Number, // 父评论ID (0表示顶级评论)
 *   content: String,       // 评论内容
 *   creationTime: String   // 创建时间
 * }
 */
export class CommentApi {

  // 查询所有评论
  static async getAllComments() {
    try {
      const response = await apiClient.get('/comment',{
        headers:{
          'Content-Type': 'application/json',
        }
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 commentID 查询评论
  static async getCommentById(commentID) {
    try {
      const response = await apiClient.get(`/comment/${commentID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 articleID 查询评论
  static async getCommentsByArticleId(articleID) {
    try {
      const response = await apiClient.get(`/comment/article/${articleID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 userID 查询评论
  static async getCommentsByUserId(userID) {
    try {
      const response = await apiClient.get(`/comment/user/${userID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 根据 parentCommentID 查询评论
  static async getCommentsByParentCommentId(parentCommentID) {
    try {
      const response = await apiClient.get(`/comment/parent/${parentCommentID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 新增评论
  static async createComment(params) {
    const { articleID, userID, parentCommentID, content, creationTime } = params;
    try {
      const response = await apiClient.post('/comment', {
        articleID,
        userID,
        parentCommentID,
        content,
        creationTime
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 更新评论
  static async updateComment(commentID, params) {
    const { articleID, userID, parentCommentID, content, creationTime } = params;
    try {
      const response = await apiClient.put(`/comment/${commentID}`, {
        commentID,
        articleID,
        userID,
        parentCommentID,
        content,
        creationTime
      });
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }

  // 删除评论
  static async deleteComment(commentID) {
    try {
      const response = await apiClient.delete(`/comment/${commentID}`);
      return response.data;
    } catch (error) {
      throw new Error(error.message);
    }
  }
}
